package com.dream.keigezhushou.Activity.acty.ksong;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.StringUtils;
import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.activity.BaseActivity;
import com.dream.keigezhushou.Activity.view.NestedScrollViewBottom;
import com.dream.keigezhushou.R;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.dream.keigezhushou.Activity.acty.ksong.ChorusInfoActivity.What.MSG;

public class ChorusInfoActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.left_back)
    ImageView leftBack;
    @BindView(R.id.tv_song_name)
    TextView tvSongName;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    @BindView(R.id.iv_splash)
    ImageView ivSplash;
    @BindView(R.id.tv_rec)
    TextView tvRec;
    @BindView(R.id.tv_total_time)
    TextView tvTotalTime;
    @BindView(R.id.tv_lrc)
    TextView tvLrc;
    @BindView(R.id.tv_restart)
    TextView tvRestart;
    @BindView(R.id.tv_author)
    TextView tvAuthor;
    @BindView(R.id.tv_finish)
    TextView tvFinish;
    @BindView(R.id.tv_rec_time)
    Chronometer tvRecTime;

    int mProgress = 0; //初始化进度条的进度
    private static final int PERCENT_TOTAL = 100; //进度条的最大值
    @BindView(R.id.scroll_bottom)
    NestedScrollViewBottom scrollBottom;
    @BindView(R.id.iv_who)
    ImageView ivWho;
    private Handler mHandler;
    private String getString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chorus_info);
        ButterKnife.bind(this);

        initView();
        initData();
    }

    //初始化界面
    protected void initView() {

//        Intent intent = getIntent();
//        getString = intent.getStringExtra(StringUtils.KEGESTATE);
//        if (getString.equals("K歌")) {
//            ivWho.setVisibility(View.GONE);
//        }

        //当进入到界面的时候，就已经开始计时
        tvRecTime.start();

        //创建录制红点闪烁的动画
        AlphaAnimation animation = new AlphaAnimation(1.0f, 0.1f);
        animation.setDuration(800); //动画时长
        animation.setFillBefore(true); //动画结束时，保持动画起始的状态
        animation.setRepeatMode(AlphaAnimation.REVERSE); //设置动画的重复的方式
        animation.setRepeatCount(AlphaAnimation.INFINITE); //设置动画的次数
        ivSplash.startAnimation(animation);

        SpannableStringBuilder builder = new SpannableStringBuilder(tvLrc.getText().toString());

        ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);

        builder.setSpan(whiteSpan, 0, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(redSpan, 0, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvLrc.setText(builder);

        mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {

                    case MSG:
                        progressBar.setProgress(mProgress);
                        break;
                    default:
                        break;
                }
            }
        };

        start();
    }


    //初始化数据
    protected void initData() {

        leftBack.setOnClickListener(this);
        tvFinish.setOnClickListener(this);
        tvRestart.setOnClickListener(this);
        tvAuthor.setOnClickListener(this);

        scrollBottom.setScrollBottomListener(new NestedScrollViewBottom.ScrollBottomListener() {
            @Override
            public void scrollBottom() {


            }
        });
    }


    //子线程的开启
    private void start() {

        new Thread(new Runnable() {
            @Override
            public void run() {

                int max = PERCENT_TOTAL;

                while (mProgress < max) {

                    mProgress += 5;
                    Message msg = new Message();
                    msg.what = MSG;
                    mHandler.sendMessage(msg);
                    try {
                        Thread.sleep(2000); //让线程沉睡2秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    //点击事件的实现

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.left_back:

                tvRecTime.stop(); //停止计时

                break;

            case R.id.tv_restart:
                tvRecTime.setBase(SystemClock.elapsedRealtime());//计时器清零
                tvRecTime.start();//重新开始计时
                break;

            case R.id.tv_author:
                break;

            case R.id.tv_finish:

                tvRecTime.stop(); //停止计时
                break;
        }
    }


    static class What {

        final static int MSG = 1;
    }
}
